package com.hnjfzn.auth.controller;

import com.hnjfzn.auth.factory.SmsLogin;
import com.hnjfzn.auth.factory.UsernameLogin;
import com.hnjfzn.auth.service.SysAuthService;
import com.hnjfzn.auth.service.SysPasswordService;
import com.hnjfzn.auth.service.SysRecordLogService;
import com.hnjfzn.common.redis.service.RedisService;
import com.hnjfzn.system.api.RemoteUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.hnjfzn.auth.form.LoginBody;
import com.hnjfzn.auth.form.RegisterBody;
import com.hnjfzn.common.core.domain.R;
import com.hnjfzn.common.core.utils.JwtUtils;
import com.hnjfzn.common.core.utils.StringUtils;
import com.hnjfzn.common.security.auth.AuthUtil;
import com.hnjfzn.common.security.service.TokenService;
import com.hnjfzn.common.security.utils.SecurityUtils;
import com.hnjfzn.system.api.model.LoginUser;

/**
 * token 控制
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private SysRecordLogService recordLogService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysAuthService authService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // TODO 传参顺序不能混乱
        LoginUser userInfo;
        if (StringUtils.isEmpty(form.getPassword())) {
            userInfo = new SmsLogin(remoteUserService, passwordService, recordLogService, redisService).login(form.getUsername());
        } else {
            userInfo = new UsernameLogin(remoteUserService, passwordService, recordLogService, redisService).login(form.getUsername(), form.getPassword());
        }
        // 获取登录  token
        return R.ok(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            authService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        authService.register(registerBody);
        return R.ok();
    }
}
